<!DOCTYPE html>
<html style="display: block;height:100%;">
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/Dao.js"></script>
		<script src="js/GuVo.js"></script>
		<script src="methods.js"></script>
		<script src="drawCharts.js"></script>
		<script src="echarts/echarts.js"></script>
		<script src="js/2022data.js"></script>
		<script src="js/2023data.js"></script>
		<script src="Strategy.js"></script>
		<script src="culGU.js"></script>
	</head>
	<body style="display: block;height:100%;" onload="init()">
		<span>
			<div id="buyInfo" style="color:red;"></div><!--买入统计信息-->
			<div id="sellInfo" style="color:green;"></div><!--卖出统计信息-->
			<div id="avg" style="color:red;"></div><!--上年统计值-->
			<div id="avg66" style="color:red;"></div><!--今年统计值-->
			<div id="avg3" style="color:red;"></div><!--每年最高统计值-->
			<div id="avg2" style="color:red;"></div><!--每年最低统计值-->
			<select id="year"></select>
			<button onclick="getData();">查询</button>
		</span>
		
		<div style="display:flex;height:500px;">
		<div id="chart2" style="width:50%;height:100%;"></div>
		<div id="chart" style="width:50%;height:100%;"></div>
		</div>
		<div style="width: 80%; display: flex;margin:auto;justify-content: space-between;">
		<table id="dd"  border="1" bordercolor="#a0c6e5" style="border-collapse:collapse;text-align: right;width:49%;"></table>
	    <table id="dd2"  border="1" bordercolor="#a0c6e5" style="border-collapse:collapse;text-align: right;width:49%;"></table>
	</div>
	</body>
</html>
<script>
	let nowPrice = 0;//当前价格
	let buyInfo = "买入信号：";//买入信息
	let sellInfo="卖出信号：";//卖出信息
	let buyParams = [0,0,0];//计算加权值[上年参考40%，今年参考30%，每年统计参考30%]
	let sellParams = [0,0,0];//计算加权值[上年参考40%，今年参考30%，每年统计参考30%]
	function init(){
		let yearUI = document.getElementById("year");
		let preYear = new Date().getFullYear()-1;
		let str= [];//年份
		for(let i = 0;i<30;i++){
		    	str.push("<option value='"+preYear+"'>"+preYear+"</option>");
				preYear--;
		}
		yearUI.innerHTML=(str.join(""));
			
		setTimeout(()=>{
			getData();
		},300)
		
		Strategy.culBuySellPoint([],1,2,{});
	}
	function getMonth(code){
		let low =[];
		let high = [];
		GuVo.getLastMonth(code,(res)=>{
			//010531,11.55,12.25,12.70,11.55,798914
			//时间，开盘价，收盘价，最高价，最低价，成交量
			 res.forEach(row=>{
				 let year = row[0].substr(0,2);
				 let year2 = low.length>0?low[low.length-1][0].substr(0,2):"";
				 
				 if(low.length==0 || year != year2){
					 low.push(row);
					 high.push(row);
				 }else{
					if(parseFloat(row[4])<parseFloat(low[low.length-1][4])){
						low[low.length-1]=row;
					}
					if(parseFloat(row[4])>parseFloat(high[high.length-1][4])){
						high[high.length-1]=row;
					}
				 }
			 });
			 initShow(low);
			 initShow2(high);
			 culResult();
			 
		});
		
	}
	function sortArr(obj){
		let arr = [];
		for(let key in obj){
			let node = {};
			node[key] = obj[key];
			arr.push(node);
		}
		arr.sort((a,b)=>{
			let aa,bb;
			for(let k in a){
				aa =a[k];
			}
			for(let k in b){
				bb =b[k];
			}
			if(aa>bb){
				return -1;
			}else if(aa<bb){
				return 1
			}else{
				return 0;
			}
			
		});
		return arr;
	}
	function initShow2(high){
		let arr = ["<tr><td>最高：时间</td><td>开盘价</td><td>收盘价</td><td>最高价</td><td>最低价</td><td>成交量</td></tr>"];
		 let key = {};
		 high.forEach(res=>{
			 key[res[0].substr(2,2)]?key[res[0].substr(2,2)]++:key[res[0].substr(2,2)]=1;
			 let str = "<tr>";
			 res.forEach(row=>{
				 str+="<td>"+row+"</td>";
			 })
			 str+="</tr>";
			 arr.push(str)
		 })
		let monthArr = sortArr(key);
		Strategy.historyMonthMaxArr = monthArr;
		document.getElementById("dd2").innerHTML = arr.join("");
		document.getElementById("avg3").innerText="每年月份高位："+JSON.stringify(monthArr);
		let month = new Date().getMonth()+1;
		month = month<10?"0"+month:month;
		for(let i=0;i<monthArr.length;i++){
			if(monthArr[i][month]){
				sellInfo+=month+"月,排名"+i+",出现次数："+monthArr[i][month]+",";
				document.getElementById("sellInfo").innerText = sellInfo;
				sellParams[2]=i==0?1:(i==1?0.666:0.333);
			}
			if(i==2){
				break;
			}
			
		}
		
	}
	function initShow(low){
		     let arr = ["<tr><td>最低：时间</td><td>开盘价</td><td>收盘价</td><td>最高价</td><td>最低价</td><td>成交量</td></tr>"];
		     let key = {};
			 low.forEach(res=>{
				 key[res[0].substr(2,2)]?key[res[0].substr(2,2)]++:key[res[0].substr(2,2)]=1;
				 let str = "<tr>";
				 res.forEach(row=>{
					 str+="<td>"+row+"</td>";
				 })
				 str+="</tr>";
				 arr.push(str)
			 })
			let monthArr = sortArr(key);
			Strategy.historyMonthMinArr = monthArr;
            document.getElementById("dd").innerHTML = arr.join("");
			document.getElementById("avg2").innerText="每年月份低位："+JSON.stringify(monthArr);
			let month = new Date().getMonth()+1;
			month = month<10?"0"+month:month;
			for(let i=0;i<monthArr.length;i++){
				if(monthArr[i][month]){
					buyInfo+=month+"月,排名"+i+",出现次数："+monthArr[i][month]+",";
					document.getElementById("buyInfo").innerText = buyInfo;
					buyParams[2]=i==0?1:(i==1?0.666:0.333);
				}
				
			}
	}
	
	function heandlePreYearData(res,code){
		let high = 0;
		let low = 9999;
		//时间，开盘价，收盘价，最高价，最低价，成交量
		res.data[code].day.map(item=>{
			if(parseFloat(item[3])>high){
				high=parseFloat(item[3]);
			}
			if(parseFloat(item[4])<low){
				low=parseFloat(item[4]);
			}
		})
		let avg = (high+low)/2;
		avg = avg.toFixed(2);
		Strategy.preYearMax = high;
		Strategy.preYearMin = low;
		document.getElementById("avg").innerText="上年,最高："+high+",最低："+low+",平均："+avg;
		if(nowPrice>high){
			sellInfo+="高于上年最高价，";
			sellParams[0]=1;
		}else if(nowPrice>avg){
			sellParams[0]=0.5;
			sellInfo+="高于上年平均价，";
		}
		if(nowPrice<low){
			buyInfo+="低于上年最低价,";
			buyParams[0]=1;
		}else if(nowPrice<avg){
			buyParams[0]=0.5;
			buyInfo+="低于上年平均价,";
		}
		document.getElementById("buyInfo").innerText = buyInfo;
		document.getElementById("sellInfo").innerText = sellInfo;
		
	}
	function handleDayData(res,code){
		console.log(res);
		let data = res.data[code].day;
		Strategy.nowYearArr = JSON.parse(JSON.stringify(data));
		let dates = [];
		let datas = [];
		data.map(res=>{
		  	dates.push(res.shift());
			datas.push(res);
		});
		nowPrice = parseFloat(datas[datas.length-1][1]);
		getMaxMin(data);
		let dom = document.getElementById("chart");
		
		let buySell = [];
		let allData = data2022.concat(data2023);
		allData.map(item=>{
			if(code.substr(2,6)==item.stkcode){
				var node = {};
				node.name = item.bsflag=="B"?"买":"卖";
				node.value = item.bsflag=="B"?"买":"卖";
				node.value = node.value+item.matchqty+"\n"+parseFloat(item.matchprice).toFixed(2);
				node.xAxis = item.bizdate;
				node.yAxis = item.matchprice;
				node.itemStyle={color:item.bsflag=="B"?"rgba(255,0,0,0.5)":"rgba(0,255,0,0.5)"}
				buySell.push(node);
			}
		})
		let i = 0;
		dates.map(item=>{
			
			buySell.map(setXaxis=>{
				if(item.split("-").join('')==setXaxis.xAxis){
					setXaxis.xAxis=i;
				}
			});
			i++;
		});
		
		drawK(dom,dates,datas,buySell);
	}
	function getData(){
		 buyInfo = "买入信号：";//买入信息
		 sellInfo="卖出信号：";//卖出信息
		let   preYear = parseInt(document.getElementById("year").value);
		var pars = decodeURI(location.href);
		
		let code =pars.split("=")[1].split("&")[0];
		document.title = pars.split("name=")[1];
		cul(code);
		let now,datePre ;
		if(new Date().getFullYear()==(preYear+1)){
			now = new Date();
			datePre = new Date((preYear+1)+'-01-01');
		}else{
			now = new Date((preYear+1)+'-12-31');
			datePre = new Date((preYear+1)+'-01-01');
		}
		
		datePre = new Date(datePre).Format("yyyy-MM-dd");
		now = now.Format("yyyy-MM-dd");
		
		let preYearData = GuVo.getDateRang(code,preYear+"-01-01",preYear+"-12-31");
		let dayDao = GuVo.getDateRang(code, datePre, now);
		Promise.all([dayDao,preYearData]).then(res=>{
			handleDayData(res[0],code);
			heandlePreYearData(res[1],code);
			getMonth(code);
		});
	}
	
	
	
	function getMaxMin(arr){
		let max=0;
		let min=99999999;
		arr.forEach(res=>{
			let r3 = parseFloat(res[2]);
			let r4 = parseFloat(res[3]);
			max = r3>max?r3:max;
			min =r4<min?r4:min;
		})
	    max=parseFloat(max);
		min=parseFloat(min);
		let avg = (max+min)*0.5;
		
		avg = avg.toFixed(2);
		document.getElementById("avg66").innerText = "今年,最高："+max+",最低："+min+",平均"+avg;
		if(nowPrice>max){
			sellInfo+="高于今年最高价，";
			sellParams[1]=1;
		}else if(nowPrice>avg){
			sellInfo+="高于今年平均价，";
			sellParams[1]=0.5;
		}
		if(nowPrice*0.95<=min){
			buyParams[1]=1;
			buyInfo+="低于今年最低价,";
		}else if(nowPrice<avg){
			buyParams[1]=0.5;
			buyInfo+="低于今年平均价,";
		}
	}
	function culResult(){
		//setTimeout(()=>{
			let str1 = buyParams[0]*40+buyParams[1]*30+buyParams[2]*30;
			let str2 = sellParams[0]*40+sellParams[1]*30+sellParams[2]*30;
			document.getElementById("buyInfo").innerText=buyInfo+str1+"%";
			document.getElementById("sellInfo").innerText=sellInfo+str2+"%";
			drawBack();
		//},500)
		
	}
	
	function drawBack(){
		
		let data = JSON.parse(JSON.stringify(Strategy.nowYearArr));
		let dates = [];
		let datas = [];
		data.map(res=>{
		  	dates.push(res.shift());
			datas.push(res);
		});
		
		let dom = document.getElementById("chart2");
		
		let buySell = [];
		let allData = Strategy.culBuySellPoint();
	
	   for(let i = 7;i<allData.length;i++){
		   let item = allData[i];
			if(item.buy>55){
				var node = {};
				
				node.value="B\n"+item.buy+"%";
				node.xAxis = item.day;
				node.yAxis=item.price;
				node.itemStyle={color:"rgba(255,0,0,0.5)"};
				buySell.push(node);
			}
			
			if(item.sell>50){
				var node = {};
				
				node.value="S\n"+item.sell+"%";
				node.xAxis = item.day;
				node.yAxis=item.price;
				node.itemStyle={color:"rgba(0,255,0,0.5)"};
				buySell.push(node);
			}
		}
		let i = 0;
		dates.map(item=>{
			
			buySell.map(setXaxis=>{
				if(item.split("-").join('')==setXaxis.xAxis){
					setXaxis.xAxis=i;
				}
			});
			i++;
		});
		
		drawK(dom,dates,datas,buySell);
	}
</script>
